<?php
namespace app\process;

use Workerman\Timer;
use Workerman\Worker;
use support\Log;
use support\Db;

class DfcfProcessor
{
    public function onWorkerStart(Worker $worker)
    {
        // 每10秒执行一次
        Timer::add(2, function() {
            $this->processBatch();
        });
    }
    
    protected function processBatch()
    {
        // 批处理逻辑 
        Log::info("111111: 耗时：".date("Y-m-d H:i:s")); 
        $hour = date('H');//9;
        $minute = date('i'); //43;  
//         if(in_array(date('w'), [6,0])){
//             echo date('Y-m-d H:i:s')."---周末无需跑！\r\n";exit;
//         }
//         if($hour < 9 || $hour >= 15 ){
//             echo date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 9 && $minute < 25){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 11 && $minute > 30 ){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 12){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }  
        $url = "https://push2ex.eastmoney.com/getAllStockChanges?type=8193,64&ut=7eea3edcaed734bea9cbfc24409ed989&pageindex=0&pagesize=64&dpt=wzchanges&_=".time().rand(100,999);
        $output = self::curlGet($url);
        $output = json_decode($output,true);  
        $tc = $output['data']['tc'];
        $page = 0;//floor($tc/64);
        for($i = 0; $i<= $page; $i++){ 
            $pageUrl =  "https://push2ex.eastmoney.com/getAllStockChanges?type=8193,64&ut=7eea3edcaed734bea9cbfc24409ed989&pageindex=$i&pagesize=64&dpt=wzchanges&_=".time().rand(100,999);
            $output1 = self::curlGet($pageUrl);
            $output1 = json_decode($output1,true);
            $allStock = $output1['data']['allstock'];
            foreach ($allStock as $stock){ 
                $info = explode(',', $stock["i"]);
                $data = [
                    'the_date' => date("Y-m-d"),
                    'hour_min_second' => $stock["tm"],
                    'code' => $stock["c"],
                    'gp_name' => $stock["n"],
                    'chengjiaoliang' => $info[0], 
                    'price' => $info[1],
                    'rise_fall' => $info[2],
                    'type' => $stock["t"], 
                ];
                //是否已经存在
                $change = Db::table('gp_daily_gupiao_changes')->where('the_date', date("Y-m-d"))
                    ->where('hour_min_second', $stock["tm"])
                    ->where('code', $stock["c"])
                    ->first();
                if($change){
                    $data['update_time'] = date("Y-m-d H:i:s");
                    Db::table('gp_daily_gupiao_changes')->where('id', $change->id)->update($data);
                    echo "【修改数据】--".$data['hour_min_second']."--".$data['code']."--".$data['gp_name']."\r\n";
                }else{
                    $data['create_time'] = date("Y-m-d H:i:s");
                    Db::table('gp_daily_gupiao_changes')->insert($data);
                    echo "新增数据--".$data['hour_min_second']."--".$data['code']."--".$data['gp_name']."\r\n";
                }
            }
        }
          
    }
    
    /**
     * curlGet
     * @param unknown $url
     * @return mixed
     */
    public static function curlGet($url){
        $ch = curl_init();
        $header [] = "push2ex.eastmoney.com"; 
        $xforip = rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 );
        $header [] = "X-FORWARDED-FOR: $xforip";
        $header [] = "CLIENT-IP: $xforip";
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_ENCODING, "" );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 ); // 设置1秒超时 
        $output = curl_exec ( $ch );
        curl_close($ch);
        return $output;
    }
}